/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package web;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author keith
 */
public class ConfigEnv {

  private static final String WEB_QUERY_EXTENSION = ".xml";

  private static final FilenameFilter WEB_QUERY_CONFIG_FILTER = new FilenameFilter(){
    /**
     * Tests if a specified file should be included in a file list.
     *
     * @param   dir    the directory in which the file was found.
     * @param   name   the name of the file.
     * @return  <code>true</code> if and only if the name should be
     * included in the file list; <code>false</code> otherwise.
     */
    public boolean accept( final File dir, final String name ){
      return name != null && name.toLowerCase().endsWith( WEB_QUERY_EXTENSION );
    }
  };

  public static ConfigEnv getConfiguration( final String envName ){
    return new ConfigEnv( envName );
  }
  
  

  transient private File configDir;
  
  /** Creates a new instance of ConfigEnv */
  private ConfigEnv( final String envName ) {
    this.configDir = new File( envName );
  }

  /** Creates a new instance of ConfigEnv */
  private ConfigEnv(){
    this( "config" );
  }

  private File[] requiredConfigFiles(){
    final List<File> list = new ArrayList<File>();

    // Cache files
//    list.add( getBivioDataFile() );
//    list.add( getMarketDataFile() );
    
    list.add( getConfigDir() );

    System.out.println( list );
    return (File[])list.toArray( new File[]{} );
  }
  
  public boolean isValid() {
    boolean valid = true;
    final File file[] = requiredConfigFiles();
    
    for ( int idx = 0, len = file.length; valid && idx < len; idx++ ){
      valid = file[ idx ].canRead();
    }
    
    return valid;
  }


  public File getConfigDir() {
    return configDir;
  }

  public File[] getWebQueries() {
    return getConfigDir().listFiles( WEB_QUERY_CONFIG_FILTER );
  }

}
